package cn.livingCloud.controller.content;

import cn.livingCloud.dto.ButtonFormListDto;
import cn.livingCloud.dto.ButtonListDto;
import cn.livingCloud.service.ButtonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.validation.Valid;

/**
 * Created by sujiantang on 2016/1/6.
 */
@Controller
@RequestMapping("/admin/wechat")
public class CustomMenuController {

    @Autowired
    private ButtonService service;

    @RequestMapping("/button_list.htm")
    public String getButtonList(ButtonListDto listDto, Model model) {
        service.findButtonList(listDto);
        model.addAttribute("listDto", listDto);
        return "wechat/menu/button_list";
    }

    @RequestMapping("/edit_button.htm")
    public String loadPage(ButtonFormListDto formListDto, Model model) {
        service.loadButtonFormDto(formListDto);
        model.addAttribute("formListDto", formListDto);
        return "wechat/menu/edit_button";
    }

    @RequestMapping("/push_menu.htm")
    public String pushMenu(Model model) {
        model.addAttribute("push", service.pushMenu());
        return "redirect:button_list.htm";
    }

    @RequestMapping(value = "/edit_button.htm", method = RequestMethod.POST)
    public String editButton(@ModelAttribute("formListDto") @Valid ButtonFormListDto formListDto, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return "wechat/menu/edit_button";
        }
        service.updateButton(formListDto);
        return "redirect:/admin/wechat/button_list.htm?edit=success";
    }

    @RequestMapping("/delete_button.htm")
    public String deleteButton(String uuid, Model model) {
        service.deleteButton(uuid);
        return "redirect:/admin/wechat/button_list.htm?delete=success";
    }
}
